package com.xnfh.planting.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.common.bean.FindFieldRecordS
import com.xnfh.common.bean.FindFieldRecordSRecord
import com.xnfh.common.bean.Record
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.framework.toast.TipsToast
import com.xnfh.planting.R
import com.xnfh.planting.databinding.FragmentFindFieldRecordsBinding
import com.xnfh.planting.ui.activity.AddXunTianActivity
import com.xnfh.planting.ui.adapter.FindFieldRecordsAdapter
import com.xnfh.planting.viewmodel.FindFieldRecordsViewModel

/**
 * @Description:巡田记录
 * @Author: ziheng
 * @CreateDate: 2023/12/15 15:38
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/15 15:38
 * @UpdateRemark: 更新说明
 */
class FindFieldRecordsFragment:  BaseFragment<FragmentFindFieldRecordsBinding>(),View.OnClickListener {
    override fun getLayoutID(): Int = R.layout.fragment_find_field_records
    companion object{
        fun getInstance(bundle: Bundle? = null): FindFieldRecordsFragment{
            val findFieldRecordsFragment = FindFieldRecordsFragment()
            if(bundle != null){
                findFieldRecordsFragment.arguments = bundle
            }
            return findFieldRecordsFragment
        }
    }
    //传入的地块信息
    private var fieldDetailBean: Record? = null
    private var findFieldRecordsViewModel: FindFieldRecordsViewModel? = null
    private var page = 1
    private var adapter:  FindFieldRecordsAdapter? = null
    override fun initView(view: View, savedInstanceState: Bundle?) {
        fieldDetailBean = arguments?.getSerializable("data") as Record
        mBind.recordsRecycle.layoutManager = LinearLayoutManager(requireContext())
        mBind.findFieldRecordsAddButton.setOnClickListener(this)

        adapter = FindFieldRecordsAdapter()
        mBind.recordsRecycle.adapter = adapter
        mBind.recordsSmart.autoRefresh()
        mBind.recordsSmart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                findFieldRecordsViewModel?.requestFindFieldRecords(fieldDetailBean?.fieldId,page)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                findFieldRecordsViewModel?.requestFindFieldRecords(fieldDetailBean?.fieldId,page)
            }
        })
        //注册子view的点击事件
        adapter?.addChildClickViewIds(R.id.item_records_delete)
        adapter?.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.item_records_delete ->{
                    XPopup.Builder(requireContext()).asConfirm(requireContext().getString(com.xnfh.framework.R.string.str_tips),requireContext().getString(R.string.pop_delete_record_tips)){
                        findFieldRecordsViewModel?.requestDelFindField((adapter.data[position] as FindFieldRecordSRecord).id.toString())
                    }.show()
                }
            }
        }
    }

    override fun observe() {
        findFieldRecordsViewModel = FindFieldRecordsViewModel()
        findFieldRecordsViewModel?.findFieldRecords?.observe(this,object :BaseStateObserver<FindFieldRecordS>(null){
            override fun getRespDataSuccess(it: FindFieldRecordS) {
                super.getRespDataSuccess(it)
                mBind.recordsSmart.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
                if(it.records.isEmpty()){
                    if(page == 1){
                        adapter?.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                        return
                    }
                    mBind.recordsSmart.setNoMoreData(true)
                    return
                }
                if(page == 1){
                    adapter?.setNewInstance(it.records.toMutableList())
                }else{
                    adapter?.addData(it.records)
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.recordsSmart.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })
        findFieldRecordsViewModel?.findFieldRecordsDel?.observe(this,object :BaseStateObserver<Any>(null){
            override fun getRespDataStart() {
                super.getRespDataStart()
                showLoadingDialog()
            }

            override fun getRespDataError() {
                super.getRespDataError()
                dismissLoadingDialog()
                TipsToast.showTips(R.string.delete_error)
            }

            override fun getRespDataSuccess(it: Any) {
                super.getRespDataSuccess(it)
                dismissLoadingDialog()
                TipsToast.showTips(R.string.delete_success)
                mBind.recordsSmart.autoRefresh()
            }
        })
    }

    override fun onClick(v: View?) {
        when(v){
            mBind.findFieldRecordsAddButton ->{
                val bundle = Bundle()
                bundle.putSerializable("data",fieldDetailBean)
                AddXunTianActivity.start(requireActivity(),AddXunTianActivity.REQUEST_CODE,bundle)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == AddXunTianActivity.REQUEST_CODE && resultCode == AddXunTianActivity.RESPONSE_CODE){
            mBind.recordsSmart.autoRefresh()
        }
    }
}